﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IRIS.Models;
namespace IRIS.Controllers
{
    public class LinkCategryController : Controller
    {
        //
        // GET: /LinkCategry/
        DataRepo rep = new DataRepo();

        [HttpGet]
        public ActionResult Index(int? id)
        {
            if(id.HasValue)
            {
                
                var model = from c in rep.getLinks()
                            where c.CID == id.Value
                            select c;
                LinkViewModel VM = new LinkViewModel {m_links = model, m_linkCategories = rep.getLinkCategories(),m_linkCategory = rep.getLinkCategoryById(id.Value)};
                return View(VM);
            }
            return View("NotFound"); 
            
        }

        [HttpGet]
        public ActionResult List()
        {
            var model = rep.getLinkCategories();
            return View(model);
        }

        //
        // GET: /LinkCategry/Create

        [Authorize(Roles = "Stjórnendur")]
        public ActionResult Create()
        {
            var lData = new LinkCategory();
            return View(lData);
        }

        //
        // POST: /LinkCategry/Create
        [Authorize(Roles = "Stjórnendur")]
        [HttpPost]
        public ActionResult Create(LinkCategory linkCategory)
        {
            //Validation for user input , Alive will be handled here also by us
            if (string.IsNullOrEmpty(linkCategory.Description))
                ModelState.AddModelError("Description", "Þessi reitur verður að vera útfylltur");
            if (string.IsNullOrEmpty(linkCategory.Name))
                ModelState.AddModelError("Name", "Þessi reitur verður að vera útfylltur");
            if(rep.getSizeOfAliveLinkCategories()>=10)
                ModelState.AddModelError("Name","Hámark 10 flokkar - eyða þarf flokki");
            
            try
            {
                if (ModelState.IsValid)
                {
                    //Everything is alright and we commit to database
                    linkCategory.Alive = true;
                    rep.addLinkCategory(linkCategory);
                    return RedirectToAction("List");
                }
                
                    //Something is wrong and we return the model back with error state
                    return View(linkCategory);
                
            }
            catch
            {
                //Something is not o.k so we return error state
                return View("Error");
            }
        }

        //
        // GET: /LinkCategry/Edit/5
        [Authorize(Roles = "Stjórnendur")]
        public ActionResult Edit(int? id, bool delete)
        {
            if (id.HasValue)
            {
                if (delete == true)
                {
                    var lData = rep.getLinkCategoryById(id.Value);
                    lData.Alive = false;
                    rep.updateLinkCategoryById(id.Value, lData);
                    return RedirectToAction("List");
                }
                var model = rep.getLinkCategoryById(id.Value);
                return View(model);
            }
           
                return View("NotFound");
            

        }

        //
        // POST: /LinkCategry/Edit/5
        // Same logic as in create
        [Authorize(Roles = "Stjórnendur")]
        [HttpPost]
        public ActionResult Edit(int? id, LinkCategory linkCategory)
        {
              
            if (string.IsNullOrEmpty(linkCategory.Name))
                ModelState.AddModelError("Name", "Nafn þarf að vera til staðar");
            if (string.IsNullOrEmpty(linkCategory.Description))
                ModelState.AddModelError("Description", "Lýsing á flokki þarf að vera til staðar");
        
            try
            {
                if (ModelState.IsValid)
                {
                    if (id.HasValue)
                    {
                        linkCategory.Alive = true;
                        rep.updateLinkCategoryById(id.Value, linkCategory);
                        return RedirectToAction("List");
                    }
                }
                return View(linkCategory);
            }
            catch
            {
                return View("Error");
            }
        }


    }
}